﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;

namespace BulldogCMS.Core.Storage.DataTypes.MVC
{
    public class MVCPresentation : BaseDataType
    {
        public string LayoutName
        {
            get;
            set;
        }

        public string AreaName
        {
            get;
            set;
        }

        public string ControllerName
        {
            get;
            set;
        }

        public string ActionName
        {
            get;
            set;
        }

        public string ViewName
        {
            get;
            set;
        }

        public override System.Xml.Linq.XElement ToXElement()
        {
            XElement element = new XElement("field");
            element.SetElementValue("areaName", this.AreaName);
            element.SetElementValue("controllerName", this.ControllerName);
            element.SetElementValue("actionName", this.AreaName);
            element.SetElementValue("viewName", this.ViewName);
            element.SetElementValue("layoutName", this.LayoutName);
            return element;
        }

        public override void LoadFromXElement(System.Xml.Linq.XElement el)
        {
            this.AreaName = el.GetElementValue<string>("areaName");
            this.ControllerName = el.GetElementValue<string>("controllerName");
            this.ActionName = el.GetElementValue<string>("actionName");
            this.ViewName = el.GetElementValue<string>("viewName");
            this.LayoutName = el.GetElementValue<string>("layoutName");
        }

        public override XElement GetMetadata()
        {
            throw new NotImplementedException();
        }

        public override dynamic ToDynamicObject()
        {
            return new { areaName = this.ActionName, controllerName = this.ControllerName, actionName = this.ActionName, viewName = this.ViewName, layoutName = this.LayoutName };
        }

        public override void LoadFromDynamicObject(dynamic obj)
        {
            try
            {
                this.AreaName =         obj.areaName;
                this.ControllerName =   obj.controllerName;
                this.ActionName =       obj.actionName;
                this.ViewName =         obj.viewName;
                this.LayoutName =       obj.layoutName;
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Parsing on MVCPresentation field failed", ex);
            }
        }
    }
}